package com.ygy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ygy.entity.Favorites;
import com.ygy.entity.Products;
import com.ygy.entity.Users;
import com.ygy.mapper.FavoritesMapper;
import com.ygy.service.impl.FavoritesServiceImpl;
import com.ygy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 收藏表 前端控制器
 * </p>
 *
 * @author Ye
 * @since 2024-03-14
 */
@RestController
@RequestMapping("/fav")
public class FavoritesController {

    @Autowired
    private FavoritesServiceImpl favoritesService;

    @Autowired
    private FavoritesMapper favoritesMapper;

    //添加收藏
    @PostMapping("/add")
    public Result addFavorites(@RequestBody Favorites favorites){
        System.out.println(favorites + "+++++++++++++====+++++++++");
        Integer user = favorites.getUserId();
        Integer product = favorites.getProductId();

        // 检查是否已经存在相同的收藏记录
        List<Favorites> list = favoritesService.list(); // 假设这个方法是获取所有收藏记录的
        boolean isExist = list.stream()
                .anyMatch(fa -> fa.getUserId().equals(user) && fa.getProductId().equals(product));
        if (isExist) {
            // 如果已存在，则返回已收藏的错误信息
            return Result.error("已收藏");
        }
        // 如果不存在，则添加新的收藏记录
        boolean add = favoritesService.save(favorites);
        if (add) {
            // 添加成功，返回成功信息和收藏的详情
            return Result.success(favorites, "收藏成功");
        } else {
            // 添加失败，返回失败信息
            return Result.error("收藏失败");
        }
    }


    /**
     * 取消收藏
     *
     * @param userId 用户ID
     * @param productId 商品ID
     * @return 响应结果
     */
    @PostMapping("/delete")
    public Result cancelFavorite(@RequestBody Favorites favorites) {
        // 创建查询条件
        Integer userId = favorites.getUserId();
        Integer productId = favorites.getProductId();

        System.out.println("用户"+userId);
        System.out.println("商品"+productId);

        QueryWrapper<Favorites> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("product_id", productId);

        // 尝试删除符合条件的收藏记录。用了mapper层接口
        int deletedCount = favoritesMapper.delete(queryWrapper);
        if (deletedCount > 0) {
            // 删除成功
            return Result.success("取消收藏成功");
        } else {
            // 没有找到对应的收藏记录或删除失败
            return (Result.error("未找到收藏记录或取消收藏失败"));
        }
    }

    //查询收藏列表
    @GetMapping("/list")
    public Result list(){

         List<Favorites> list = favoritesService.list();

         return Result.success(list);
    }

    //同时判断用户有无收藏该商品
    @PostMapping("/cf")
    public Result checkFavorite(@RequestBody Favorites favorites) {
        // 创建查询条件
        Integer userId = favorites.getUserId();
        Integer productId = favorites.getProductId();
        System.out.println("用户"+userId);
        System.out.println("商品"+productId);
        System.out.println("创建查询条件");
        QueryWrapper<Favorites> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("product_id", productId);
        // 尝试查询符合条件的收藏记录
        Favorites favorite = favoritesMapper.selectOne(queryWrapper);
        if (favorite != null) {
            // 找到了收藏记录
            return Result.success(favorites, "用户已收藏该产品");
        } else {
            // 没有找到对应的收藏记录
            return Result.error("用户未收藏该产品");
        }
    }


    @GetMapping("/products/{user_id}")
    public Result<Page<Products>> getFavoriteProductsByUserId(
            @PathVariable int user_id,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        Page<Products> productPage = favoritesService.getFavoriteProductsByUserId(user_id, pageNum, pageSize);
        return Result.success(productPage);
    }


}
